// lib/ui/order/order_detail_page.dart
import 'package:flutter/material.dart';
import 'package:flutter_deer/database/beans/order/order_list_bean_entity.dart';
import 'package:flutter_deer/res/colors.dart';
import 'package:flutter_deer/ui/cashier/provider/common_http_provider.dart';
import 'package:flutter_deer/util/other_utils.dart';
import 'package:flutter_deer/widgets/dialog/password_input_dialog.dart';
import 'package:get/get.dart';
import 'package:get/get_core/src/get_main.dart';

class OrderDetailPage extends StatefulWidget {
  final OrderListBeanList? order;

  const OrderDetailPage({super.key, this.order});

  @override
  State<OrderDetailPage> createState() => _OrderDetailPageState();
}

class _OrderDetailPageState extends State<OrderDetailPage> {
  final commonHttpProvider = Get.find<CommonHttpProvider>();

  @override
  Widget build(BuildContext context) {
    if (widget.order == null) {
      return _buildEmptyView();
    }

    return Container(
      padding: const EdgeInsets.all(20),
      decoration: BoxDecoration(
        color: Colors.white,
        borderRadius: BorderRadius.circular(12),
        boxShadow: [
          BoxShadow(
            color: Colors.grey.withOpacity(0.1),
            blurRadius: 8,
            offset: const Offset(0, 2),
          ),
        ],
      ),
      child: Column(
        crossAxisAlignment: CrossAxisAlignment.start,
        children: [
          // 标题行
          Row(
            children: [
              const Text('订单详情', style: TextStyle(fontSize: 20, fontWeight: FontWeight.bold, color: Colours.text)),
              const SizedBox(width: 20),
              // 操作按钮
              ElevatedButton(
                onPressed: () {
                  // 处理订单操作，例如取消订单、联系客服等
                  _handleRefund();
                },
                child: const Text(' 退 款 '),
              ),
            ],
          ),
          const SizedBox(height: 20),

          // 订单基本信息
          _buildInfoRow('订单号', widget.order!.orderId ?? ''),
          const SizedBox(height: 10),
          _buildInfoRow('订单类型', widget.order!.orderType.toString()),
          const SizedBox(height: 10),
          _buildInfoRow('订单状态', widget.order!.orderStatus.toString()),
          const SizedBox(height: 10),
          _buildInfoRow('客户类型', widget.order!.clientTypeName ?? ''),
          const SizedBox(height: 10),
          _buildInfoRow('手机号', widget.order!.phone ?? ''),
          const SizedBox(height: 10),
          _buildInfoRow('订单金额', '¥${widget.order!.orderAmount}'),
          const SizedBox(height: 10),
          _buildInfoRow('订单时间', widget.order!.orderTime ?? ''),
        ],
      ),
    );
  }

  Widget _buildInfoRow(String label, String value) {
    return Row(
      crossAxisAlignment: CrossAxisAlignment.start,
      children: [
        SizedBox(
          width: 80,
          child: Text(
            '$label:',
            style: const TextStyle(
              fontSize: 14,
              fontWeight: FontWeight.w500,
              color: Colors.grey,
            ),
          ),
        ),
        const SizedBox(width: 10),
        Expanded(
          child: Text(
            value,
            style: const TextStyle(
              fontSize: 14,
              color: Colours.text,
            ),
          ),
        ),
      ],
    );
  }

  Widget _buildEmptyView() {
    return Center(
      child: Column(
        mainAxisAlignment: MainAxisAlignment.center,
        children: [
          Icon(
            Icons.info_outline,
            size: 80,
            color: Colors.grey[400],
          ),
          const SizedBox(height: 16),
          Text(
            '请选择订单查看详情',
            style: TextStyle(
              fontSize: 18,
              color: Colors.grey[600],
            ),
          ),
          const SizedBox(height: 8),
          Text(
            '在左侧列表中选择一个订单以查看详细信息',
            style: TextStyle(
              fontSize: 14,
              color: Colors.grey[500],
            ),
          ),
        ],
      ),
    );
  }

  // 退款操作处理方法
  // 退款操作处理方法
  void _handleRefund() {
    // 显示密码输入对话框
    _showPasswordDialog();
  }

  // 显示密码输入对话框
  void _showPasswordDialog() {
    showDialog<void>(
      context: context,
      barrierDismissible: false, // 防止点击背景关闭对话框
      builder: (BuildContext mContext) {
        return PasswordInputDialog(
          onPasswordValidated: (password) {
            // 密码验证成功，执行退款操作
            commonHttpProvider.orderRefund(
              orderId: widget.order!.orderId ?? '',
              password: password.toMd5,
              success: (data) {
                // 退款成功，刷新订单详情
                ScaffoldMessenger.of(context).showSnackBar(
                  const SnackBar(content: Text('退款操作已执行，请稍后查看订单状态')),
                );
              },
              onError: (code, msg) {
                // Toast.show('退款操作失败 code: $code, msg: $msg');
              },
            );
          },
        );
      },
    );
  }
}
